package tencent

import (
	"context"
	"fmt"
	sms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sms/v20190711"
)

type Service struct {
	appId     *string
	signature *string
	client    *sms.Client
}

func NewSmsService() Service {
	return Service{
		appId:     nil,
		signature: nil,
		client:    &sms.Client{},
	}
}
func (s *Service) Send(ctx context.Context, tpl string, args []string, number ...string) error {
	request := sms.NewSendSmsRequest()
	request.SmsSdkAppid = s.appId
	request.TemplateID = &tpl
	request.PhoneNumberSet = s.toStringPtrSlice(number)
	request.TemplateParamSet = s.toStringPtrSlice(args)
	response, err := s.client.SendSms(request)
	if err != nil {
		return err
	}
	for _, v := range response.Response.SendStatusSet {
		if v.Code == nil || *v.Code == "Ok" {
			return fmt.Errorf("发送短信失败:%s %s", *v.Code, *v.Message)
		}
	}
	return nil
}
func (s *Service) toStringPtrSlice(src []string) []*string {
	var res []*string
	for _, v := range src {
		res = append(res, &v)
	}
	return res
}
